package com.ww.testclient.order;

import com.google.common.collect.Lists;
import com.ww.microrpc.netty.handler.client.SendCallback;
import com.ww.testclient.order.feignApi.OrderServiceFeignApi;
import com.ww.testclient.order.rpcApi.OrderServiceApi;
import com.ww.testclient.order.util.OrderData;
import com.ww.testserver.order.OrderEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderServiceApi orderService;
    @Autowired
    private OrderServiceFeignApi orderServiceFeignApi;

    @Autowired
    private ApplicationEventPublisher publisher;

    /**
     * 订单提交：同步提交
     */
    @GetMapping("/submit")
    public OrderEntity submitOrderByNetty(){
        OrderEntity order = new OrderEntity();
        order.setOrderId(0);
        order.setUserId("");
        order.setOrderData(OrderData.msg10);
        OrderEntity orderEntity = orderService.submitOrder(order);
        return orderEntity;
    }

    /**
     * 订单提交：异步提交
     */
    @GetMapping("/submit/async")
    public void submitOrderByNettyAsync(){
        OrderEntity order = new OrderEntity();
        order.setOrderId(0);
        order.setUserId("");
        order.setOrderData(OrderData.msg10);
        orderService.submitOrder(order, new SendCallback() {
            @Override
            public void onSuccess(Object result) {
                OrderEntity orderEntity = (OrderEntity) result;
            }

            @Override
            public void onException(Throwable e) {
                System.out.println("出错了...");
                e.printStackTrace();
            }
        });
    }

    /**
     * 使用feign进行提交
     */
    @GetMapping("/http/submit")
    public OrderEntity submitOrderByHttp(){
        OrderEntity order = new OrderEntity();
        order.setOrderId(1);
        order.setUserId("qweqweqweqasdadsasd");
        order.setOrderData("12312easdasdqweqweqweqwe");
        order.setMessage(Lists.newArrayList("ww", "gg", "ll"));
        OrderEntity orderEntity = orderServiceFeignApi.submitOrder(order);
        return orderEntity;
    }

    @GetMapping("/batch/submit")
    public Object batchSubmitOrderByNetty(@RequestParam("num") Integer num){
        Map<String, String> hashmap = new HashMap<>(8);
        List<OrderEntity> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            OrderEntity order = new OrderEntity();
            order.setOrderId(1);
            order.setUserId("a");
            order.setOrderData(OrderData.msg10);
            order.setMessage(Lists.newArrayList("ww", "gg", "ll"));
            list.add(order);
        }

        long l = System.currentTimeMillis();
        for (OrderEntity orderEntity : list) {
            orderService.submitOrder(orderEntity);
        }
        long s = System.currentTimeMillis();
        long netty = s - l;
        for (OrderEntity orderEntity : list) {
            orderServiceFeignApi.submitOrder(orderEntity);
        }
        long http = System.currentTimeMillis() - s;
        hashmap.put("netty", netty + "");
        hashmap.put("http", http + "");
        return hashmap;
    }
}
